import com.base.config.BaseConfig
import com.base.model.api.UserModel_Admin
import org.apache.ibatis.annotations.Delete
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update
import org.springframework.stereotype.Repository

@Mapper
@Repository
interface MySQLMapper_Admin {
    @Select("""
        SELECT
            u.id id
        FROM
            t_user_admin u
        WHERE
            u.loginname=#{username} and u.password=#{password}
    """)
    fun admin_getUserByAccount(username: String, password: String): List<Map<String, Any?>>?

    @Select("""
        SELECT
            u.name name,
            r.menu_models menuModels
        FROM
            t_user_admin u inner join t_user_role r
        WHERE
            u.role=r.id and u.id=#{uid}
    """)
    fun admin_getUserInfoByUID(uid: String): List<UserModel_Admin>?

    @Select("""
        SELECT
            r.jkids jkids
        FROM
            t_user_admin u inner join t_user_role r
        WHERE
            u.role=r.id and u.id=#{uid}
    """)
    fun admin_getJkidsByUid(uid: String): List<Map<String, Any?>>

    @Select("""
        <script>
            SELECT
                COUNT(1)
            FROM
                t_spu s
            <if test = "searchContent != null and searchContent != ''">
                WHERE s.title LIKE CONCAT('%', #{searchContent}, '%')
            </if>
        </script>
    """)
    fun admin_getSPUTotal(searchContent: String?): Int

    @Select("""
        <script>
            SELECT
                s.id id,
                s.title title,
                CONCAT('${BaseConfig.ossBaseURL}', s.img_main) img,
                CONCAT('${BaseConfig.ossBaseURL}', s.video) video,
                CONCAT('${BaseConfig.ossBaseURL}', s.img_banner) img_banner,
                CONCAT('${BaseConfig.ossBaseURL}', s.img_detail) img_detail,
                s.count_sale,
                s.des des,
                s.money_min money_min,
                s.money_max money_max,
                s.content_like content_like,
                s.params_pinpai params_pinpai,
                s.params_xinghao params_xinghao,
                s.params_tuihuan params_tuihuan
            FROM
                t_spu s
            <if test = "searchContent != null and searchContent != ''">
                WHERE s.title LIKE CONCAT('%', #{searchContent}, '%')
            </if>
            LIMIT 
                #{limit}, #{pageSize}
        </script>
    """)
    fun admin_getSPUs(limit: Int, pageSize: Int, searchContent: String?): List<Map<String, Any>>?

    @Insert("""
        INSERT INTO
            t_spu
            (title, img_main, video, img_banner, img_detail, des, content_like, params_pinpai, params_xinghao, params_tuihuan)
        VALUES
            (#{title}, #{img_main}, #{video}, #{img_banner}, #{img_detail}, #{des}, #{content_like}, #{params_pinpai}, #{params_xinghao}, #{params_tuihuan})
    """)
    fun admin_addSPU(
            title: String,
            img_main: String,
            video: String?,
            img_banner: String?,
            img_detail: String?,
            des: String?,
            content_like: String?,
            params_pinpai: String?,
            params_xinghao: String?,
            params_tuihuan: String,
    ): Int

    @Update("""
        UPDATE
            t_spu s
        SET
            s.title=#{title},
            s.des=#{des},
            s.content_like=#{content_like},
            s.params_pinpai=#{params_pinpai},
            s.params_xinghao=#{params_xinghao},
            s.params_tuihuan=#{params_tuihuan}
        WHERE
            s.id=#{id}
    """)
    fun admin_updateSPU(
            id: String,
            title: String,
            des: String?,
            content_like: String?,
            params_pinpai: String?,
            params_xinghao: String?,
            params_tuihuan: String,
    ): Int

    @Delete("""
        DELETE FROM
            t_spu
        WHERE
            id=#{id}
    """)
    fun admin_deleteSPU(id: String): Int
}